//
//  LSRThemePictureView.m
//  BaiSiProject
//
//  Created by lshenrong on 16/7/14.
//  Copyright © 2016年 lshenrong. All rights reserved.
//

#import "LSRThemePictureView.h"
#import <UIImageView+WebCache.h>
#import "LSRThemeItem.h"
#import <DALabeledCircularProgressView.h>
#import <SDImageCache.h>
#import "LSRSeeBigPictureController.h"
@class LSRThemePictureView;
@interface LSRThemePictureView()
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;

@property (weak, nonatomic) IBOutlet UIImageView *middleImageView;

@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigPicture;

@end

@implementation LSRThemePictureView

#pragma  mark---点击图片查看大图
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

   
    LSRSeeBigPictureController *seeBigPicture=[[LSRSeeBigPictureController alloc] init];
    seeBigPicture.item=self.item;
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:seeBigPicture animated:YES completion:nil];


}

+(instancetype)cellThemePicture
{

    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self)    owner:nil options:nil] lastObject];



}
-(void)awakeFromNib{

    [super awakeFromNib ];
    self.progressView.progressLabel.textColor=[UIColor whiteColor];
    
    self.progressView.roundedCorners=10;

}
-(void)setItem:(LSRThemeItem *)item


{
    //保存起来，保证self.item 有值
    _item=item;
    //运用SDImageCache类保存起来、 // SDWebImage以图片URL为key

    UIImage *image=[[SDImageCache sharedImageCache] imageFromDiskCacheForKey:item.image0];
    //服务器数据问题，image0,改为image1,所有数据都会显示了
    [self.middleImageView sd_setImageWithURL:[NSURL URLWithString:item.image1] placeholderImage:image options:SDWebImageRetryFailed|SDWebImageLowPriority  progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        float progress=1.0*receivedSize/expectedSize;
        NSLog(@"%f",progress);

        self.progressView.progressLabel.text=[NSString stringWithFormat:@"%.1f%%",progress*100];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
       
        if(!item.isBigPicture)return ;//不用else,用return可读性强一些
        //开启图片上下文
        CGFloat pictureW=LSRScreenW-20;
        CGFloat pictrueH=(pictureW/item.width)*item.height;
        
        CGRect frame=CGRectMake(0, 0, pictureW, pictrueH);
        //        UIGraphicsBeginImageContext(frame.size);
        
       //
     UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0);
        //绘制
        [image drawInRect:frame];
        //获取图片
        image=UIGraphicsGetImageFromCurrentImageContext();
        
        //关闭上下文
        UIGraphicsEndImageContext();
        
        _middleImageView.image=image;
        
    }];
    //后面多了complete模块就可以针对下载的那孩子那个图片就行操作，修改了
    
    //如果是gif就不隐藏
    self.gifImageView.hidden=!item.is_gif;
    //如果是大图，点击查看大图按钮就隐藏
    self.seeBigPicture.hidden=!item.isBigPicture;
    
    if (item.isBigPicture) {
        self.middleImageView.contentMode=UIViewContentModeTop;
        self.middleImageView.clipsToBounds=YES;//裁剪还是能看到溢出的图片
    }else
    {
        
        //还原
        self.middleImageView.contentMode=UIViewContentModeScaleToFill;
        self.middleImageView.clipsToBounds=NO;
        
        
    }

}

@end
